/*
 * =====================================================================================
 *
 *       Filename:  utils_net.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2018年07月21日 16时23分51秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef _UTIL_NET_H
#define _UTIL_NET_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>


/* interfaces */
#define LEN_IP  4
#define LEN_MAC 6
#define LEN_IP_STR  (sizeof("255.255.255.255"))
#define LEN_MAC_STR (sizeof("FF:FF:FF:FF:FF:FF"))

#define IP_STR_FMT "%u.%u.%u.%u"
#define IP_STR_ARG(ptr) \
    ((uint8_t *)ptr)[0], ((uint8_t *)ptr)[1], ((uint8_t *)ptr)[2], ((uint8_t *)ptr)[3] 
#define MAC_STR_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
#define MAC_STR_ARG(ptr) \
    ((uint8_t *)ptr)[0], ((uint8_t *)ptr)[1], ((uint8_t *)ptr)[2], \
    ((uint8_t *)ptr)[3], ((uint8_t *)ptr)[4], ((uint8_t *)ptr)[5]

#define SAME_MAC(x, y) \
   (((((uint8_t *)x)[0]) == (((uint8_t *)y)[0])) && \
    ((((uint8_t *)x)[1]) == (((uint8_t *)y)[1])) && \
    ((((uint8_t *)x)[2]) == (((uint8_t *)y)[2])) && \
    ((((uint8_t *)x)[3]) == (((uint8_t *)y)[3])) && \
    ((((uint8_t *)x)[4]) == (((uint8_t *)y)[4])) && \
    ((((uint8_t *)x)[5]) == (((uint8_t *)y)[5])))

#define SAME_IP(x, y) \
   (((((uint8_t *)x)[0]) == (((uint8_t *)y)[0])) && \
    ((((uint8_t *)x)[1]) == (((uint8_t *)y)[1])) && \
    ((((uint8_t *)x)[2]) == (((uint8_t *)y)[2])) && \
    ((((uint8_t *)x)[3]) == (((uint8_t *)y)[3])))

#define SAME_NETWORK(ip1,ip2,mask) \
    (((ip1) & (mask)) == ((ip2) & (mask)))

int get_all_interfaces();
int get_eth_u32_ip(const char *eth_name, uint32_t *ip);
int get_eth_str_ip(const char *eth_name, char *ip, int len);
int get_eth_str_mac(const char *eth_name, char *mac, int len);
int get_eth_u8_mac(const char *eth_name, uint8_t *mac, int len);
int get_eth_u32_dstip(const char *eth_name, uint32_t *dstip);
int get_eth_str_dstip(const char *eth_name, char *dstip, int len);
int get_eth_u32_netmask(const char *eth_name, uint32_t *mask);
int get_eth_str_netmask(const char *eth_name, char *mask, int len);
int get_eth_u32_broadcast(const char *eth_name, uint32_t *broadcast);
int get_eth_str_broadcast(const char *eth_name, char *broadcast, int len);
int get_eth_mtu(const char *eth_name);
short get_eth_flags(const char *eth_name);
int get_eth_metric(const char *eth_name);
int get_eth_network(const char *eth_name, char *network, int len);
int netmask_str_to_u32(const char *netmask, uint32_t *u32_netmask);
int netmask_str_to_bits(const char *netmask);
int netmask_bits_to_u32(int bits, uint32_t *u32_netmask);
int netmask_bits_to_str(int bits, char *netmask, int len);
int netmask_u32_to_bits(uint32_t u32_netmask);
int netmask_u32_to_str(uint32_t u32_netmask, char *netmask, int len);

int tcpsock_create(const char *ip, unsigned short port, int backlog);
int tcpsock_connect(const char *ip, unsigned short port);
int tcpsock_connect_timeout(const char *ip, unsigned short port, int timeout);
int sock_set_nonblock(int fd);
int sock_set_reuseaddr(int _fd);
int sock_set_reuseport(int _fd);
int sock_get_peeraddr(int fd, char *ip, unsigned short *port);
int sock_get_localaddr(int fd, char *ip, unsigned short *port);

int udpsock_create();
int udpsock_bind(int fd, const char *_ip, unsigned short _port);
int udpsock_connect(int fd, const char *ip, unsigned short port);



#endif

